package com.netease.framework.db;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.provider.BaseColumns;

/**
 * sqlite3 数据库支持
 * @author Panjf
 * @date   2011-10-9
 */
public class DBSupport extends SQLiteOpenHelper {

    public  static final String AUTHORITY = "com.netease.project.framedb";
    private static final int VERSION = 1;
    private static final String NAME = "framedb.db";
    
    private static DBSupport sDbInstance;
    
    synchronized public static DBSupport getInstance(Context ct){
    	if(sDbInstance == null){
    		sDbInstance = new DBSupport(ct);
    	}
    	
    	return sDbInstance;
    }
    private DBSupport(Context ct) {
        super(ct, NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        try {
            StringBuilder buf = new StringBuilder();

            buf.append("CREATE TABLE ");
            buf.append(PreferenceTable.TABLE_NAME);
            buf.append(" (");
            buf.append(PreferenceTable._ID);
            buf.append(" INTEGER PRIMARY KEY AUTOINCREMENT,");
            buf.append(PreferenceTable.BELONGTO);
            buf.append(" TEXT,");
            buf.append(PreferenceTable.KEY);
            buf.append(" TEXT NOT NULL,");
            buf.append(PreferenceTable.VALUE);
            buf.append(" TEXT");
            buf.append(");");
            
            db.execSQL(buf.toString());
            
            buf.delete(0, buf.length());
            buf.append("CREATE INDEX key_idx ON ");
            buf.append(PreferenceTable.TABLE_NAME);
            buf.append("(");
            buf.append(PreferenceTable.KEY);
            buf.append(");");
            db.execSQL(buf.toString());
            buf.delete(0, buf.length());
            
            buf.append("CREATE INDEX belong_idx ON ");
            buf.append(PreferenceTable.TABLE_NAME);
            buf.append("(");
            buf.append(PreferenceTable.BELONGTO);
            buf.append(");");
            db.execSQL(buf.toString());
            buf.delete(0, buf.length());           
           
            
            //统计表
            buf.delete(0, buf.length());
            buf.append("CREATE TABLE ");
            buf.append(CollectorTable.TABLE_NAME);
            buf.append(" (");
            buf.append(CollectorTable._ID);
            buf.append(" INTEGER PRIMARY KEY AUTOINCREMENT,");
            buf.append(CollectorTable.COLLECTOR);
            buf.append(" TEXT");
            buf.append(");");
            db.execSQL(buf.toString());
            buf.delete(0, buf.length());
            
        } catch (SQLiteException e) {
            e.printStackTrace();
        }
    }

   
    
    
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        switch (oldVersion) {
            case 1:
                if (1 >= newVersion) {
                    return;
                }
                return;
        }

        destroyOldTables(db);
        onCreate(db);
    }

    
    private void destroyOldTables(SQLiteDatabase db) {

        try {
            db.execSQL("DROP TABLE IF EXISTS " + CollectorTable.TABLE_NAME);
            db.execSQL("DROP TABLE IF EXISTS " + PreferenceTable.TABLE_NAME);
        } catch (SQLiteException e) {
            e.printStackTrace();
        }
    }
    
    /********************************************************
     * 
     * 设置首选项
     *
     ********************************************************/
    public static final class PreferenceTable implements BaseColumns{
    	public static String BELONGTO = "belongto";
    	public static String KEY = "key";
    	public static String VALUE = "value";
    	
    	private PreferenceTable(){
    		
    	}
        
    	public static final String TABLE_NAME = "preference";
        public static final Uri Preference_URI = Uri.parse("content://" + AUTHORITY
                + "/" + TABLE_NAME);
    }
    

    /********************************************************
     * 
     * 存储统计信息
     *
     ********************************************************/
    public static final class CollectorTable implements BaseColumns{
    	public static String COLLECTOR = "collector";
    	
    	private CollectorTable(){
    		
    	}
        
    	public static final String TABLE_NAME = "collector";
        public static final Uri Collector_URI = Uri.parse("content://" + AUTHORITY
                + "/" + TABLE_NAME);
    }
    
 
}
